package com.ruoyi.project.communication.service.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.communication.mapper.UserCommunicationMapper;
import com.ruoyi.project.communication.domain.UserCommunication;
import com.ruoyi.project.communication.service.IUserCommunicationService;

/**
 * 用户交流Service业务层处理
 * 
 * @author Daniel
 * @date 2020-04-03
 */
@Service
public class UserCommunicationServiceImpl implements IUserCommunicationService 
{
    @Autowired
    private UserCommunicationMapper userCommunicationMapper;

    /**
     * 查询用户交流
     * 
     * @param ucId 用户交流ID
     * @return 用户交流
     */
    @Override
    public UserCommunication selectUserCommunicationById(Long ucId)
    {
        return userCommunicationMapper.selectUserCommunicationById(ucId);
    }

    /**
     * 查询用户交流列表
     * 
     * @param userCommunication 用户交流
     * @return 用户交流
     */
    @Override
    public List<UserCommunication> selectUserCommunicationList(UserCommunication userCommunication)
    {
        return userCommunicationMapper.selectUserCommunicationList(userCommunication);
    }

    @Override
    public List<UserCommunication> getRoot()
    {
        final List<UserCommunication> root = userCommunicationMapper.getRoot();
        for(UserCommunication uc:root){
            uc.setReply(getReply(uc));
        }
        return root;
    }

    private List<UserCommunication> getReply(UserCommunication uc){
        UserCommunication temp=new UserCommunication();
        temp.setUcPId(uc.getUcId());
        List<UserCommunication> list = userCommunicationMapper.selectUserCommunicationList(temp);
        if(list.size()>0){
            for(UserCommunication u:list){
                u.setReply(getReply(u));
            }
        }
        return list;
    }

    /**
     * 新增用户交流
     * 
     * @param userCommunication 用户交流
     * @return 结果
     */
    @Override
    public int insertUserCommunication(UserCommunication userCommunication)
    {
        userCommunication.setLikeNum((long) 0);
        userCommunication.setUserAvatar(SecurityUtils.getLoginUser().getUser().getAvatar());
        userCommunication.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        userCommunication.setUserName(SecurityUtils.getLoginUser().getUser().getNickName());

        return userCommunicationMapper.insertUserCommunication(userCommunication);
    }

    /**
     * 修改用户交流
     * 
     * @param userCommunication 用户交流
     * @return 结果
     */
    @Override
    public int updateUserCommunication(UserCommunication userCommunication)
    {
        return userCommunicationMapper.updateUserCommunication(userCommunication);
    }

    /**
     * 批量删除用户交流
     * 
     * @param ucIds 需要删除的用户交流ID
     * @return 结果
     */
    @Override
    public int deleteUserCommunicationByIds(Long[] ucIds)
    {
        return userCommunicationMapper.deleteUserCommunicationByIds(ucIds);
    }

    /**
     * 删除用户交流信息
     * 
     * @param ucId 用户交流ID
     * @return 结果
     */
    @Override
    public int deleteUserCommunicationById(Long ucId)
    {
        return userCommunicationMapper.deleteUserCommunicationById(ucId);
    }
}
